﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PBOffice.BLL;
using PBOffice.Models;

public partial class SystemManager_RoleUser : System.Web.UI.Page
{
    public static int roleId;
    List<Branch> branchList = new BranchManager().GetAllBranch();
    List<Department> deptList = new DeptManager().GetAllDepartment();
    List<Position> list = new PosManager().GetAllPos();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            TreeView1.Attributes.Add("checkchange", "tree_oncheck(this)");
            BindData();
            roleId = Convert.ToInt32(Request.QueryString["id"]);
            List<RoleStaff> list = new RoleStaffManager().GetRoleStfByRoleId(roleId);
            foreach (RoleStaff role in list)
            {

                foreach (TreeNode item in this.TreeView1.Nodes)
                {
                    processNode(item, role.staff.StfName);
                }
            }


        }
    }
    private void processNode(TreeNode tn, string name)//这里会遍历所有的子节点
    {

        foreach (TreeNode item in tn.ChildNodes)
        {
            if (item.Text.Trim() == name)
            {
                item.Checked = true;
            }
            processNode(item, name);
        }

    }
    private void BindData()
    {

        foreach (Branch item in branchList)
        {

            TreeNode tn = new TreeNode(item.BranchShortName);
            tn.Value = item.BranchId.ToString();
            tn.ImageUrl = "~/imges/dept_tree.jpg";
            this.TreeView1.Nodes.Add(tn);
            InitTreeByList(tn);

        }
    }
    private void InitTreeByList(TreeNode TN)
    {
        foreach (Department item in deptList)
        {
            if (item.ParentId.ToString() == TN.Value)
            {
                TreeNode tn = new TreeNode();
                tn.Text = "&nbsp;&nbsp;" + item.DeptName;
                tn.Value = item.DeptId.ToString();
                List<Position> list = new PosManager().GetPosByDept(Convert.ToInt32(item.DeptId));
                if (list.Count != 0)
                {
                    foreach (Position pos in list)
                    {
                        TreeNode t = new TreeNode();
                        t.Text = pos.PosName;
                        t.Value = pos.PosId.ToString();
                        List<Staff> sta = new StaffManager().GetNameByPosIdAndDeptId(item.DeptId, pos.PosId);
                        if (sta.Count != 0)
                        {
                            foreach (Staff aa in sta)
                            {
                                TreeNode s = new TreeNode();
                                s.Text = aa.StfName;
                                s.Value = aa.StfId.ToString();
                                s.ImageUrl = "~/imges/people.jpg";
                                t.ChildNodes.Add(s);
                            }
                        }
                        tn.ChildNodes.Add(t);
                    }
                }
                //tn.NavigateUrl = item.NodeURL;
                // tn.ImageUrl = "~/imges/dept_tree_block.jpg";
                TN.ChildNodes.Add(tn);
                InitTreeByList(tn);
            }


        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        new RoleStaffManager().DeleteRoleStaff(roleId);
        foreach (TreeNode item in this.TreeView1.Nodes)
        {
            processNode1(item);
        }
    }
    private void processNode1(TreeNode tn)//这里会遍历所有的子节点
    {

        foreach (TreeNode item in tn.ChildNodes)
        {
            foreach (Staff stf in new StaffManager().GetAllStaff("", "", ""))
            {
                if (item.Checked == true && item.Text.Trim() == stf.StfName)
                {
                    RoleStaff role = new RoleStaff();
                    Staff staff = new Staff();
                    staff.StfName = item.Text;
                    staff.StfId = Convert.ToInt32(item.Value);
                    role.CreateTime = DateTime.Now;
                    role.RoleId = roleId;
                    role.staff = staff;
                    new RoleStaffManager().AddRoleStaff(role);

                }
            }

            processNode1(item);
        }

    }
}